using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace HostpitalDevice
{
    public partial class DeviceTypeFiled : UserControl
    {
        DataTable m_table;

        public DeviceTypeFiled()
        {
            
            InitializeComponent();
            
        }

        public void LoadTable(DataTable t)
        {
            m_table = t;
        }

        private void txt_Input_TextChanged(object sender, EventArgs e)
        {
            string expression = "name like '%" + this.txt_Input.Text + "%'";
            DataRow[] foundRows;

            // Use the Select method to find all rows matching the filter.
            foundRows = m_table.Select(expression);
            /// DataTable tmp = new DataTable(foundRows);
            this.list_Result.Items.Clear();
            foreach (DataRow r in foundRows)
            {
                list_Result.Items.Add(r[0]);
            }

            if (list_Result.Items.Count > 1)
            {
                this.list_Result.Top = this.txt_Input.Top + this.txt_Input.Height;
                this.list_Result.Left = this.txt_Input.Left;
                this.list_Result.Visible = true;
            }
            else
            {
                this.list_Result.Visible = false;
            }
        }

   
        private void txt_Input_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down && this.list_Result.Items.Count!=0)
            {
                this.list_Result.Focus();
            }
        }

        private void list_Result_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.list_Result.Visible = false;
                this.txt_Input.Text = this.list_Result.SelectedItem.ToString();
            }
        }
    }
}
